/*
 * Copyright (C) 2012 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.ohos.camera.open;

import ohos.agp.utils.TextTool;
import ohos.eventhandler.EventHandler;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.camera.CameraKit;
import ohos.media.camera.device.Camera;
import ohos.media.camera.device.CameraInfo;
import ohos.media.camera.device.CameraStateCallback;

import java.util.logging.Logger;

public final class OpenCameraInterface {

    private static final HiLogLabel label = new HiLogLabel(HiLog.WARN, 0x00001, OpenCameraInterface.class.getName());

    /**
     * For {@link #open(CameraKit, String, CameraStateCallback, EventHandler)}, means no preference for which camera to open.
     */
    public static final String NO_REQUESTED_CAMERA = "";
    private static final String TAG = "OpenCameraInterface";

    public static String getCameraId(CameraKit cameraKit, String requestedId) {
        String[] cameraIds = cameraKit.getCameraIds();
        int numCameras = cameraIds.length;
        if (numCameras == 0) {
            HiLog.warn(label,"No cameras!");
            return null;
        }

        String cameraId = requestedId;

        boolean explicitRequest = !TextTool.isNullOrEmpty(cameraId);

        int index = -1;
        if (!explicitRequest) {
            // Select a camera if no explicit camera requested
            CameraInfo cameraInfo;
            index = 0;
            while (index < numCameras) {
                cameraInfo = cameraKit.getCameraInfo(cameraIds[index]);
                if (cameraInfo.getFacingType() == CameraInfo.FacingType.CAMERA_FACING_BACK) {
                    break;
                }
                index++;
            }

            cameraId = cameraIds[index];
        }

        if (index < numCameras) {
            return cameraId;
        } else {
            if (explicitRequest) {
                return null;
            } else {
                return cameraIds[0];
            }
        }
    }

    /**
     * Opens the requested camera with {@link Camera#open(int)}, if one exists.
     *
     * @param requestedId camera ID of the camera to use. A negative value
     *                    or {@link #NO_REQUESTED_CAMERA} means "no preference"
     * @return handle to {@link Camera} that was opened
     */
    public static void open(CameraKit cameraKit, String requestedId, CameraStateCallback cameraStateCallback, EventHandler eventHandler) {
        Logger.getLogger(TAG).warning("open");
        String cameraId = getCameraId(cameraKit, requestedId);
        Logger.getLogger(TAG).warning("open - " + cameraId);
        if (!TextTool.isNullOrEmpty(cameraId)) {
            cameraKit.createCamera(cameraId,cameraStateCallback,eventHandler);
        }
    }
}
